﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.Mvc.JQueryValidation
{
	public class MaxAttribute : ValidatorAttribute
	{
		private int maxValue;

		public override string JQueryRule
		{
			get
			{
				return "max: " + maxValue;
			}
		}

		public override string JQueryRuleErrorMessage
		{
			get
			{
				return "max: \"" + JQueryErrorMessage + "\"";
			}
		}

		public override bool Validate(string value)
		{
			var intValue = 0;
			return String.IsNullOrEmpty(value) || (!String.IsNullOrEmpty(value) && int.TryParse(value, out intValue) && intValue <= maxValue);
		}

		public MaxAttribute(string fieldName, int maxValue)
			: base(String.Empty, fieldName)
		{
			this.maxValue = maxValue;
		}

		public MaxAttribute(string formID, string fieldName, int maxValue)
			: base(formID, fieldName)
		{
			this.maxValue = maxValue;
		}
	}
}